<%-- 
    Document   : SimpleAddAccount
    Created on : Apr 21, 2010, 2:18:02 AM
    Author     : AaronFrost
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="powerpass.PowerPassSQL" %>
<%@page import="java.sql.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<style type="text/css">
input{
    background-color: silver;
}

</style>

<SCRIPT LANGUAGE="JavaScript">
function checkPasswordMatch()
{
    var pass = document.getElementById("password");
    var confpass = document.getElementById("confirmpassword");
    if (pass.value == null || pass.value == "")
    {
        alert("You can't leave the password blank.");
        pass.focus();
    }
    else if (pass.value != confpass.value)
    {
        alert("The passwords don't match. Please try again.");
        pass.value="";
        confpass.value="";
        pass.style.backgroundColor='#FF6699';
        confpass.style.backgroundColor='#FF6699';
        pass.focus();
    }
    else
    {
        //alert("The passwords DO match.");
        pass.style.backgroundColor="lightgreen";
        confpass.style.backgroundColor="lightgreen";
        enableSubmitButton();
    }
}
function disableSubmitButton()
{
    //alert("made it in");
    var submitbutton = document.getElementById("submit")
    //submitbutton.setAttribute('readonly',true);
    submitbutton.disabled=true;
    //alert("made it out");
}
function enableSubmitButton()
{
    var submitbutton = document.getElementById("submit");
    submitbutton.disabled=false;
    submitbutton.focus();
}
function highlightFieldRed(fieldid)
{
    document.getElementById(fieldid).style.backgroundColor="MistyRose";
}
</SCRIPT>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="table.css" />
        <link rel="stylesheet" href="csstg.css" type="text/css">
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Add Account</title>
    </head>
    <body onload="setTitle_AddAcount();disableSubmitButton();">
        <%
        
        System.out.println(request.getParameter("accountname"));
        System.out.println(request.getParameter("accountdescription"));
        System.out.println(request.getParameter("username"));
        System.out.println(request.getParameter("password"));
        
        
        if (request.getParameter("accountname") != null &&
            request.getParameter("accountdescription") != null &&
            request.getParameter("username") != null &&
            request.getParameter("password") != null )
        {
            if (!request.getParameter("accountname").isEmpty() &&
                !request.getParameter("accountdescription").isEmpty() &&
                !request.getParameter("username").isEmpty() &&
                !request.getParameter("password").isEmpty() )
            {
                String sqlClientID = (String) session.getAttribute("clientid");
                String sqlAccountName = (String) request.getParameter("accountname");
                String sqlAccountDescription = (String) request.getParameter("accountdescription");
                String sqlUsername = (String) request.getParameter("username");
                String sqlPassword = (String) request.getParameter("password");
                String newSQL = "INSERT INTO accounts (clientid, active, createddt, modifieddt, name, username, description, accounttype) "+
                                "VALUES ("+ sqlClientID +", true, now(), now(),'"+ sqlAccountName +"', '"+sqlUsername +"', '"+ sqlAccountDescription +"', 1);" ;
                PowerPassSQL pp = new PowerPassSQL();
                pp.insertInfo(newSQL);

                newSQL = "Select max(accountid) from accounts where clientid = "+ sqlClientID +";";
                ResultSet rs = pp.selectInfo(newSQL);
                rs.next();
                newSQL = "INSERT INTO accountpasshistory (accountid, password, startdate, enddate) " +
                        "VALUES ("+ rs.getString(1) +", '" + sqlPassword +"', now(), '0000-00-00 00:00:00');";
                pp.insertInfo(newSQL);
                out.write("ACCOUNT ADDED SUCCESSFULLY!");%><BR><%
                out.write("ClientID: "+ sqlClientID);%><BR><%
                out.write("AccountName: "+ sqlAccountName);%><BR><%
                out.write("AccountDesc: "+ sqlAccountDescription);%><BR><%
                out.write("Username: "+ sqlUsername);%><BR><%
                out.write("Password: "+ sqlPassword);
                pp.Close();
                %><jsp:forward page="passPage.jsp?page=dash"/><%
            }
            else
            {
                pageContext.setAttribute("accountname", request.getAttribute("accountname"));
                pageContext.setAttribute("accountdescription", request.getAttribute("accountdescription"));
                pageContext.setAttribute("username", request.getAttribute("username"));

                if(request.getParameter("accountname").isEmpty())
                {
                    %>Cannot leave Account Name Blank<BR><%
                }
                if(request.getParameter("accountdescription").isEmpty())
                {
                    %>Cannot leave Account Description blank<BR><%
                }
                if(request.getParameter("username").isEmpty())
                {
                    %>Cannot leave Username blank<BR><%
                }
                if(request.getParameter("password").isEmpty())
                {
                    %>Cannot leave Password blank<%
                }
            }
        }
        

        %>
        <form id="myform" action="SimpleAddAccount.jsp">
            <table
                <tr>
                    <td >Account Name:</td>
                    <td ><input name="accountname" id="accountname" type="text" size="22"/></td>
                </tr>
                <tr>
                    <td >Account Description:</td>
                    <td ><input name="accountdescription" id="accountdescription" type="text" size="22"/></td>
                </tr>
                <tr>
                    <td >Username:</td>
                    <td ><input name="username" id="username" type="text" size="22"/></td>
                </tr>
                <tr>
                    <td >Password:</td>
                    <td ><input name="password" id ="passsword" type="text" size="22"/></td>
                </tr>
                <tr>
                    <td >Confirm Password:</td>
                    <td ><input name="confirmpassword" id ="confirmpassword" type="text" size="22" onblur="checkPasswordMatch()"/></td>
                </tr>
            </table>
            <table>
                <tr >
                    <td><input name="reset" type="Reset" value="RESET"/></td>
                    <td></td>
                    <td><input name="submit" id="submit" type="Submit" value="SUBMIT"/></td>
                    <td></td>
                    <td><input name="submit" id="submit" type="Submit" value="SUBMIT" readonly style="visibility:hidden;"/></td>
                </tr>
            </table>
            
        </form>
        <script type="text/javascript" language="javascript">
               document.getElementById("accountname").focus();
        </script>
    </body>
</html>
